#ifndef APPLICATION_H
#define APPLICATION_H

#define MAX_INSTANCES 5

#include <QString>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QtDebug>
#include <windows.h>
#include <tchar.h>
#include "..\commons\commons.h"
//#include "client.h"

class ApplicationManager;
class Client;

class Application : public QObject{

	Q_OBJECT



/* Gestisce il ciclo di vita di una applicazione */
public:
	Application(QString name, ApplicationManager *mng);
	~Application();
	bool run(QString param);
	bool stop();
	QString getLog();
	Status getStatus();
	QString getStatusString();
	QString getName();
	
	void deleteFiles();
public slots:
	void gestisciStateChanged(QProcess::ProcessState newState);
private:
	static const QStringList statusString;
//	QProcess *istances[MAX_INSTANCES];
//	int ninstance;
	QProcess *process;
	QFile *log;
	QFile *application;
	QString name;
	Status status;
	ApplicationManager *appMng;
	
};


class ApplicationManager 
{
	
public:
	ApplicationManager(int client_id, Client *clientRef);
	~ApplicationManager();
	bool installApp(QString name);
	bool uninstallApp(QString name);
	bool runApplication(QString name, QString args);
	bool stopApplication(QString name);
	QString getStatusString(quint16 port);
	QString getLogsString(QStringList *applications, quint16 port);
	HANDLE getJobHndl();
	void updateStatus();
	bool applicationExists(QString appName);
private:
	QMap<QString, Application *> applicazioni;
	int client_id;
	HANDLE jobHnd;
	Client *clientRef;
};

#endif
